function [X, L, X_err, L_err, iter, flag] = ... 
    arnoldi(A, Xo, X_ref, L_ref, maxit, tol, print)
%
%  [X,L,X_err,L_err,iter,flag] = arnold(A, Xo, X_ref, L_ref, maxit, tol)
%
%  ARNOLDI applies the Arnoldi method to the eigenvalue problem
%

if (nargin==6)
    print = 1;
end

k = 2;         % number of eigenvalues desired
m = 20;        % maximum krylov subspace dimension (m > 2*k recommended)
%  largest magnitued: LM
%  largest algebraic: LA
%  largest real part: LR)
which = 'LM'; % want largest magnitude
[XX, LL, iter] = Iram(A,which,k,m,Xo,X_ref, L_ref);

LL = diag(LL);                   % ritz values
[LL,idx] = sort(LL,1,'descend'); % sort them, high to low, and save index 
L = LL(idx(1));                  % take maximum ritz value
X = XX(:,idx(1));                %   and associated ritz vector

% compute errors
X_err = norm(X - sign(X(1)/X_ref(1))*X_ref, 2);
L_err = abs(L - L_ref);

if (print==1) 
    disp(' *** IRAM: Final Results *** '); 
end

if (print==1) 
    disp([' Iter = ',num2str(iter), ' X_err = ',num2str(X_err), ...
          ' Lambda = ', num2str(L), ' L_err = ',num2str(L_err)]); 
end

flag = 0;

end

